/*
 * Copyright 2007 Gergely Kis
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.gwthtml.servlet;

import java.io.FileReader;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom.Document;

/**
 * Servlet implementation class for Servlet: HelloServlet
 *
 */
 public class GWTServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = 5300698921176439084L;
	private static final String MODE_HTML = "html";
	private static final String MODE_NORMAL = "normal";

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public GWTServlet() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String mode = getMode(request);
		if (mode.equals(MODE_HTML)) {
			executeHtmlRequest(request, response);
		}
		else {
			executeNormalRequest(request, response);
		}
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String mode = getMode(request);
		if (mode.equals(MODE_HTML)) {
			executeHtmlRequest(request, response);
		}
		else {
			executeNormalRequest(request, response);
		}
	}
	
	private void executeHtmlRequest(HttpServletRequest request, HttpServletResponse response) {
	    synchronized (request.getSession()) {
			  GWTContext.initContext(request, response);
			  
			  boolean requestDone = false;
			  try {
				  GWTContext.execute(this);
			  } catch (GWTRequestDone gex) {
				  requestDone = true;
			  }

			  GWTContext.commitContext(requestDone);
		}		
	}
	
	private String getMode(HttpServletRequest request) {
		String mode;
		if ((mode = request.getParameter("mode")) != null) {
			// We have a potential mode selection
			if (mode.equals("")) {
				mode = MODE_NORMAL;
			}
			if (mode.equals(MODE_HTML) || mode.equals(MODE_NORMAL)) {
				request.getSession().setAttribute("GWT-Mode", mode);
			}
		} else {
			mode = (String) request.getSession().getAttribute("GWT-Mode");
			if (mode == null) {
				mode = MODE_NORMAL;
				request.getSession().setAttribute("GWT-Mode", mode);
			}
		}
		return mode;
	}
	
	private void executeNormalRequest(HttpServletRequest request, HttpServletResponse response) {
		Document descriptor = GWTContext.parseGWTModuleDescription(this, getServletConfig().getInitParameter("GWT-ModuleName"));
		String path = GWTContext.getGWTPlaceholderPath(descriptor, this);
		FileReader fr;
		try {
			fr = new FileReader(path);
			int b;
			while ((b = fr.read()) != -1) {
				response.getWriter().write(b);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

 
}